/*
 Copyright (c) 2010, Advanced Micro Devices
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modification, are permitted 
 provided that the following conditions are met:
 
 Redistributions of source code must retain the above copyright notice, this list of conditions 
 and the following disclaimer.
 
 Redistributions in binary form must reproduce the above copyright notice, this list of 
 conditions and the following disclaimer in the documentation and/or other materials provided 
 with the distribution.
 
 Neither the name of Advanced Micro Devices nor the names of its contributors may be used to endorse 
 or promote products derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef NODE_H
#define NODE_H

#include <QApplication>
#include <QColor>
#include <QGraphicsItem>
#include <QSet>

#include <stdlib.h>
#include <iostream>
#include <fstream>

class Link;
class EditorWindow;
class Graph;

#include "EditorView.h"


class Node : public QGraphicsItem
{
    Q_DECLARE_TR_FUNCTIONS(Node)

public:
    Node( EditorView *, EditorWindow * );
    ~Node();

    void setText(const QString &text);
    QString text() const;
    void setTextColor(const QColor &color);
    QColor textColor() const;
    void setOutlineColor(const QColor &color);
    QColor outlineColor() const;
    void setBackgroundColor(const QColor &color);
    QColor backgroundColor() const;
	
#if 0
    QRectF outlineRect() const;
#else
    virtual QRectF outlineRect() const;
#endif
	
    void addLink(Link *link);
    void removeLink(Link *link);
	Link * findLink( Node * );
	void createTree() const;

    QRectF boundingRect() const;
#if 0
    QPainterPath shape() const;
#else
	virtual QPainterPath shape() const;
#endif
    void paint(QPainter *painter,
               const QStyleOptionGraphicsItem *option, QWidget *widget);
	virtual void propertiesDialog() = 0;
	virtual void writeXml( QXmlStreamWriter & ) const;
	virtual void readXml( QXmlStreamReader & );
	void restoreLinks( EditorWindow * );
	virtual QString typeOf(){ return ""; }
	virtual void build( std::ofstream & file ){}

	QString name() const
	{
		return mName;
	}
	void nameIs( QString name )
	{
		mName = name;
	}
	EditorView * view() const
	{
		return mView;
	}
	void viewIs( EditorView * view )
	{
		mView = view;
	}
	EditorWindow * window() const
	{
		return mWindow;
	}
	void windowIs( EditorWindow * window )
	{
		mWindow = window;
	}
	QString checkStateString( Qt::CheckState checkState ) const
	{
		switch( checkState ){
			case Qt::Unchecked: return "Unchecked"; break;
			case Qt::PartiallyChecked: return "PartiallyChecked"; break;
			case Qt::Checked: return "Checked"; break;
		}//switch
		return "";
	}
	Qt::CheckState stringCheckState( QString string )
	{
		if( string.compare( "Unchecked" ) == 0 ){
			return Qt::Unchecked;
		}
		else if( string.compare( "PartiallyChecked" ) == 0 ){
			return Qt::PartiallyChecked;
		}
		else if( string.compare( "Checked" ) == 0 ){
			return Qt::Checked;
		}
		return (Qt::CheckState)0;
	}
	QString qreal2qstring( qreal r ) const
	{
#if 0
		int decpt;
		int sign;
		return QString( _fcvt( r, 5, &decpt, &sign ));
#else
		char buffer[ 80 ];
		sprintf( buffer, "%g", r );
		return QString( buffer );
#endif
	}

	qreal qstring2qreal( QString s ) const
	{
#if 0
		qreal r;
		sscanf( (const char *)s.data(), "%g", (float *)&r );
		return r;
#else
		return (qreal)s.toFloat();
#endif
	}
	Graph * graph() const{ return mGraph; }
	void graphIs( Graph * graph ){ mGraph = graph; }
	virtual void recordGraph( Graph * graph ){}
	
protected:
    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) = 0;
    QVariant itemChange(GraphicsItemChange change,
                        const QVariant &value);

public:
    int roundness(double size) const;

    QSet<Link *> myLinks;
    QString myText;
    QColor myTextColor;
    QColor myBackgroundColor;
    QColor myOutlineColor;

private:
	QString mName;
	EditorView * mView;
	EditorWindow * mWindow;
	Graph * mGraph;
};

#endif
